mui.init();
$(document).ready(function() {
 	var cookie=  $.AMUI.utils.cookie;
 	var sessionId=(cookie.get("sessionId")==null)?"":cookie.get("sessionId");
 	var factoryId=(cookie.get("factoryid")==null)?"":cookie.get("factoryid");
	$.getJSON("json/locat.json", function(data) {
		var  factoryid=data["factoryid"];
		//mui.toast(sessionId+"为什么？？？");
		/*if(null == sessionId || "undefined" == sessionId || "" == sessionId){
			window.location.href="login.html";
		} else{*/
			var locat = data["url"];
			$.ajax(locat+"/appClient/to_appUser",{
			dataType: 'json',
			type: 'POST',
			timeout: 100000,
			xhrFields: {
	                withCredentials: true,
	                useDefaultXhrHeader: false
	            },
	            crossDomain: true,
			success: function(data) {
				if("" == data["jsonListFactory"]){
					window.location.href = "login.html";
				}else{
					var finallist = "<a> <i class='am-icon-file am-margin-left-sm'></i> "
					+ "网上下单(选择以下工厂) <i class='am-icon-angle-down am-fr am-margin-right'></i>"
					+ "</a>";
					for(i = 0 ; i <= data["jsonListFactory"].length - 1 ; i++) {
						finallist += "<ul class='am-list admin-sidebar-sub am-in' id='user-nav'>"
					+ "<li><a href='cwddlb.html?factoryId="+ data["jsonListFactory"][i]["id"]+"'> "
					+ "<i class='am-icon-home am-margin-left-sm'></i>"
					+ data["jsonListFactory"][i]["factoryName"] +"&nbsp;<span>(销售货单)</span></a></li></ul>";
					
					/*+ "<ul class='am-list admin-sidebar-sub am-in' id='user-nav'>"
					+ "<li><a href='cwddlb.html?factoryId="+ data["jsonListFactory"][i]["id"]+"'> "
					+ "<i class='am-icon-home am-margin-left-sm'></i>"
					+ data["jsonListFactory"][i]["factoryName"] +"&nbsp;<span>(采购货单)</span></a></li></ul>"*/
					
					}
					$("#factoryList").append(finallist);
				}
				
				
			},
			error: function() {
				sweetAlert("服务异常", "请稍后再试", "error");
				window.location.href = "login.html";
				
			}
			})
		//}
			
		
		//订单列表，如果没有登录。先进行登录
		
	

	});

})

function loginout(){
	var cookie=  $.AMUI.utils.cookie;
 	var sessionId=(cookie.get("sessionId")==null)?"":cookie.get("sessionId");
	swal({
		title: "您确定要退出吗？",
		text: "",
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: false,
		cancelButtonText: "取消",
		confirmButtonText: "确定",
		confirmButtonColor: "#ec6c62"
	}, function() {
		$.getJSON("json/locat.json", function(data) {
	
		var locat = data["url"];
		//如果没有登录。先进行登录
		
		$.ajax(locat+'/clientUser/logout?sessionId='+sessionId, {
			dataType: 'json',
			type: 'get',
			timeout: 100000,
			xhrFields: {
	                withCredentials: true,
	                useDefaultXhrHeader: false
	            },
	            crossDomain: true,
			success: function(data) {
				
				if("success" == data["mes"]){
					cookie.set("sessionId","");
				window.location.href = "login.html";
				}
				
			},
			error: function() {
				sweetAlert("服务异常", "请稍后再试", "error");
				window.location.href = "index.html";
				
			}
		});

	});
		
		
	});
	
	
}
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}